package com.freemyleft.left.zapp.selectimage;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.freemyleft.left.R;


/**
 * Created by Administrator on 2017/3/19.
 */

public abstract class BaseDialogUtils extends Dialog {


    private Activity mActivity;
    private int mLayoutId;
    private View mView;
    private float mRatio= 1;
    public BaseDialogUtils(Activity activity, int layoutId) {
       this(activity, layoutId, 1);
    }
    public BaseDialogUtils(Activity activity, int layoutId, float ratio) {
        super(activity, R.style.UIAlertViewStyle);
        mRatio= ratio;
        mActivity = activity;
        mLayoutId = layoutId;
        show();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inite();
    }
    public void inite() {
        LayoutInflater inflater = LayoutInflater.from(mActivity);
         mView = inflater.inflate(mLayoutId, null);
        setContentView(mView);

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = mActivity.getResources().getDisplayMetrics();
        lp.width = (int) (d.widthPixels * mRatio);
        setCanceledOnTouchOutside(true);
        dialogWindow.setAttributes(lp);
        if (convert(new Holder())){
            mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
    }
    /**
     * 没有设置点击事件，点击之后是否消失
     * */
    protected abstract boolean convert(Holder holder);

    public class Holder {
        public <T extends View> T getView(int viewId) {
            View view = mView.findViewById(viewId);
            return (T) view;
        }

        public TextView setText(int viewId, String str) {
            TextView textView = getView(viewId);
            textView.setText(str);
            return textView;
        }
    }


}
